package com.wang.springbootinit.controller.admin;

import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.model.dto.TrainingQueryDTO;
import com.wang.springbootinit.model.dto.TrainingUploadDTO;
import com.wang.springbootinit.service.TrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 培训管理后台控制器
 * 提供培训课程的增删改查等管理功能
 */
@RestController
@RequestMapping("/admin/training")
public class AdminTrainingController {
    @Autowired
    private TrainingService trainingService;

    /**
     * 添加新的培训课程
     * @param itemName 培训课程名称
     * @param trainingContent 培训内容
     * @param video 视频文件
     * @return 添加成功的培训课程信息
     */
    @PostMapping("/add")
    public BaseResponse addTraining(
            @RequestParam("itemName") String itemName,
            @RequestParam("trainingContent") String trainingContent,
            @RequestParam(value = "video", required = false) MultipartFile video, HttpServletRequest request) {
        
        TrainingUploadDTO uploadDTO = new TrainingUploadDTO();
        uploadDTO.setItemName(itemName);
        uploadDTO.setTrainingContent(trainingContent);
        if (video != null && !video.isEmpty()) {
            // 处理视频文件，可以将文件信息设置到 uploadDTO 中
            uploadDTO.setVideo(video);
        }
        
        return ResultUtils.success(trainingService.addTraining(uploadDTO,request));
    }

    /**
     * 更新现有培训课程信息
     * @param uploadDTO 更新的培训课程信息
     * @return 更新后的培训课程信息
     */
    @PostMapping("/update")
    public BaseResponse updateTraining(@RequestParam("trainingId") Long trainingId,
                                       @RequestParam("itemName") String itemName,
                                       @RequestParam("trainingContent") String trainingContent,
                                       @RequestParam(value = "video", required = false) MultipartFile video, HttpServletRequest request) {
        TrainingUploadDTO uploadDTO = new TrainingUploadDTO();
        uploadDTO.setTrainingId(trainingId);
        uploadDTO.setItemName(itemName);
        uploadDTO.setTrainingContent(trainingContent);
        uploadDTO.setVideo(video);
        return ResultUtils.success(trainingService.updateTraining(uploadDTO,request));
    }

    /**
     * 删除指定培训课程
     * @param trainingId 培训课程ID
     * @return 删除操作结果
     */
    @PostMapping("/delete/{trainingId}")
    public BaseResponse deleteTraining(@PathVariable("trainingId") Long trainingId) {
        return ResultUtils.success(trainingService.deleteTraining(trainingId));
    }

    /**
     * 分页查询培训课程列表
     * @param queryDTO 查询条件传输对象
     * @return 分页后的培训课程列表
     */
    @GetMapping("/page")
    public BaseResponse getTrainingPage(TrainingQueryDTO queryDTO) {
        return ResultUtils.success(trainingService.getTrainingPage(queryDTO));
    }

    /**
     * 根据ID获取培训课程详情
     * @param trainingId 培训课程ID
     * @return 培训课程详细信息
     */
    @GetMapping("/get/{id}")
    public BaseResponse getTrainingById(@PathVariable("id") Long trainingId) {
        return ResultUtils.success(trainingService.getTrainingById(trainingId));
    }
} 